package com.mall.order.service.imp;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.mall.manager.dao.TbOrderItemMapper;
import com.mall.manager.dao.TbOrderMapper;
import com.mall.manager.dao.TbOrderShippingMapper;
import com.mall.manager.pojo.TbOrderItem;
import com.mall.manager.pojo.TbOrderShipping;
import com.mall.order.pojo.OrderInfo;
import com.mall.order.service.OrderService;
import com.mall.pojo.TaotaoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @author Administrator
 */
@Service(timeout = 10000000)
@Component
@Transactional
public class OrderServiceImp implements OrderService {

    @Autowired
    private TbOrderMapper orderMapper;

    @Autowired
    private TbOrderShippingMapper orderShippingMapper;

    @Autowired
    private TbOrderItemMapper orderItemMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${order_ID}")
    private String ORDER_ID;

    @Value("${order_ID_NUM}")
    private long ORDER_ID_NUM;

    @Value("${ORDER_ITEM_ID}")
    private String ORDER_ITEM_ID;

    @Value("${cart_prefix}")
    private String CART_PREFIX;


    @Override
    public TaotaoResult createOrder(OrderInfo orderInfo) {
        //生成订单号
        Boolean hashId = stringRedisTemplate.hasKey(ORDER_ID);
        if (!hashId){
            //设置订单初始单号
            stringRedisTemplate.opsForValue().set(ORDER_ID, String.valueOf(ORDER_ID_NUM));
        }

        long orderId = stringRedisTemplate.opsForValue().increment(ORDER_ID, 1);

        //插入订单等相关信息
        orderInfo.setOrderId(orderId + "");
        //订单支付状态 1：未付款
        orderInfo.setStatus(1);
        Date date = new Date();
        orderInfo.setCreateTime(date);
        orderInfo.setUpdateTime(date);
        //插入订单表
        orderMapper.insert(orderInfo);

        //插入order-item表
        List<TbOrderItem> orderItems = orderInfo.getOrderItems();
        for (TbOrderItem orderItem : orderItems) {
            //设置订单跟商品编号的id
            long orderItemId = stringRedisTemplate.opsForValue().increment(ORDER_ITEM_ID, 1);
            //补全order-item的id号
            orderItem.setId(orderItemId + "");
            //设置orderId
            orderItem.setOrderId(orderId + "");
            //插入order-item表
            orderItemMapper.insert(orderItem);
        }

        TbOrderShipping orderShipping = orderInfo.getOrderShipping();
        //补全物流表
        orderShipping.setCreated(date);
        orderShipping.setUpdated(date);
        //设置orderId
        orderShipping.setOrderId(orderId + "");

        //插入地址信息
        orderShippingMapper.insert(orderInfo.getOrderShipping());

        //将订单编号返回
        return TaotaoResult.ok(orderId);
    }

    @Override
    public void deleteAllCartItemsByUserId(long userId) {
        stringRedisTemplate.delete(CART_PREFIX + userId);
    }
}
